package com.chinasoft.mall.activity.subActivity.goods;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.BaseActivity;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.activity.adapter.SpListAdapter;
import com.chinasoft.mall.framework.BeanCache;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.GoodsByClassBean;
import com.chinasoft.mall.model.response.RechnBean;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.widget.LoadListView;
import com.chinasoft.mall.widget.LoadListView.OnRefreshListener;
import com.chinasoft.mall.widget.NetImageView;

public class GoodsListActivity extends BaseActivity implements
		OnRefreshListener {

	private LoadListView modellist;
	private BaseBean goodsbyclass_bb;
	private SpListAdapter mlAdapter;
	private List<GoodsByClassBean> gbcb = new ArrayList<GoodsByClassBean>();;
	private NetImageView imageView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sy_more_modellist2);
		initView();
		// if (goodsbyclass_bb == null ) {
		// gbcb = new ArrayList<GoodsByClassBean>();
		// }

	}

	@Override
	protected void onResume() {
		super.onResume();
		initRequest();

	}

	private void initRequest() {
		goodsbyclass_bb = BeanCache.getInstance().getResponsdata()
				.get(Config.GET_GOODSBYCLASS);
		if (goodsbyclass_bb != null) {
			success(goodsbyclass_bb);
		} else {

			Toast.makeText(this, "获取数据失败", 500).show();
//			goodsbyclass_bb = new BaseBean(Config.GET_GOODSBYCLASS);
//			goodsbyclass_bb.reqData.put("pageNo", "1");
//			goodsbyclass_bb.reqData.put("pageSize", "10");
//			goodsbyclass_bb.reqData.put("class_id", ((RechnBean) sz).getID());
//			control.sendMessage(goodsbyclass_bb);
		}

	}

	private void initView() {
		gbcb.clear();
		modellist = (LoadListView) findViewById(R.id.modellist);
		mlAdapter = new SpListAdapter((MainActivity) this.getParent(), null);
		modellist.setAdapter(mlAdapter);
		modellist.setonRefreshListener(this);
		imageView = (NetImageView) findViewById(R.id.focus);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if (obj.equals(goodsbyclass_bb)) {
			if (obj.getResData() != null && obj.getResData().getData() != null) {
				gbcb.addAll((List<GoodsByClassBean>) obj.getResData().getData());
				mlAdapter.setData(gbcb);
				modellist.onRefreshComplete();
				if (((List<GoodsByClassBean>) obj.getResData().getData())
						.size() > 0) {
					imageView.setDownloadurl(StringUtil.goodFocusUrl(
							((List<GoodsByClassBean>) obj.getResData()
									.getData()).get(0).getCLASS_ID(),
							((List<GoodsByClassBean>) obj.getResData()
									.getData()).get(0).getFOCUS()));
				}
			}

		}
	}

	@Override
	public void fail(BaseBean obj) {
		super.fail(obj);
		Toast.makeText(this, "获取数据失败", 500).show();
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}

	@Override
	public boolean equals(Object o) {
		// TODO Auto-generated method stub
		return super.equals(o);
	}

	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return super.hashCode();
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString();
	}

	@Override
	public void onRefresh() {
		goodsbyclass_bb.reqData.put("pageNo",
				(Integer.parseInt(goodsbyclass_bb.reqData.get("pageNo")) + 1)
						+ "");
		control.postMessage(goodsbyclass_bb);
	}
}
